package com.NHL.modules.mc.animation
{
	import com.NHL.modules.mc.vo.AnimationVo;
	import com.NHL.single.AnimationLoader;
	import com.NHL.single.ESource;
	import com.NHL.utils.StrUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
	public class McAnimation extends Sprite
	{
		[Embed (source="mc.jpg") ]
		private static var _defaultBmd:Class;
		private static var _bmd:BitmapData;
		private var _data:Vector.<AnimationVo>;
		private var _classId:String;
		private var _curFrame:int;
		private var _maxFrame:int;
		private var _bmp:Bitmap;
		private var _curAni:AnimationVo;
		private var _actType:String;
		private var _actDirArr:Array;
		private var _actActArr:Array;
		protected var _actlength:int = 4;
		private static const DIR:int = 8;
		private var _dataLength:int;
		public function McAnimation(classId:String)
		{
			super();
			_bmp = new Bitmap();
			addChild(_bmp);
			_dataLength = _actlength*DIR;
			_data = new Vector.<AnimationVo>;
			_classId = classId;
			if(_data.length == 0)
			{
				var animation:Vector.<AnimationVo> = ESource.getInstance().getAnimation(_classId);
				if(animation&&animation.length == _dataLength)
				{
					_data = animation;
				}
				else
				{
					var url:String = StrUtil.makeAnimationURL(classId);
					AnimationLoader.getInstance().loadArr = [url];
					AnimationLoader.getInstance().start(loadComplete);
				}
			}
			
			if(_bmd==null)
			{
				_bmd = new _defaultBmd();
			}
			
		}
		
		public function get actlength():int
		{
			return _actlength;
		}
		
		public function set actlength(value:int):void
		{
			_actlength = value;
		}
		
		public function get actActArr():Array
		{
			return _actActArr;
		}
		
		public function set actActArr(value:Array):void
		{
			_actActArr = value;
		}
		
		public function get actDirArr():Array
		{
			return _actDirArr;
		}
		
		public function set actDirArr(value:Array):void
		{
			_actDirArr = value;
		}
		
		public function get data():Vector.<AnimationVo>
		{
			return _data;
		}
		
		public function set data(value:Vector.<AnimationVo>):void
		{
			_data = value;
		}
		
		public function get actType():String
		{
			return _actType;
		}
		
		public function set actType(value:String):void
		{
			_actType = value;
			setVo(_actType);
		}
		
		private function setVo(str:String):void
		{
			var arr:Array = str.split("_");
			
			if(_data.length == _dataLength)
			{			
				var firstIndex:int = _actActArr.indexOf(arr[0]);
				var secondIndex:int = _actDirArr.indexOf(arr[1]);
				_curAni = _data[firstIndex*_actActArr.length + secondIndex];
			}
			else
			{
				
			}
		}
		
		public function updateFrame():void
		{
			if(_data.length != _dataLength)
			{
				_bmp.bitmapData =  _bmd;
				return;
			}
			if(_curAni)
			{
				_curAni = _data[0];
			}
			_curFrame++;
			if(_curFrame >= _maxFrame)
			{
				_curFrame = 0;
				
			}
			_bmp.bitmapData = _curAni.data[_curFrame];
		}
		
		private function loadComplete():void
		{
			var animation:Vector.<AnimationVo> = ESource.getInstance().getAnimation(_classId);
			if(animation)
			{
				_data = animation;
			}
		}
		
	}
}